package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.OtherPersonHomeActivity;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.domain.SubscribeMyObj;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;

import java.util.List;

public class SubscribeMyAdapter extends BaseAdapter {
	private final String TAG = "SubscribeMyAdapter";
	List<SubscribeMyObj> infos;
	Context context;
	private int mHeight;
	private int mWidth;

	public SubscribeMyAdapter(Context context, List<SubscribeMyObj> infos) {
		this.infos = infos;
		this.context = context;
		this.mWidth = Utils.computeImageWidthTo3(context);
		this.mHeight = Utils.computeImageHeightWithWidth(mWidth);
		Logger.i(TAG, "mWidth==" + mWidth + "mHeight=" + mHeight);
	}

	public void updata(List<SubscribeMyObj> infos) {
		if (this.infos != null) {
			this.infos.clear();
			this.infos.addAll(infos);
		} else {
			this.infos = infos;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView != null && convertView instanceof LinearLayout)
			holder = (ViewHolder) convertView.getTag();
		else {
			holder = new ViewHolder();
			convertView = View.inflate(context,
					R.layout.fragment_subscribe_item_my, null);
			holder.imgHead = (ImageView) convertView
					.findViewById(R.id.my_scribe_user_img);
			holder.imgV = (ImageView) convertView
					.findViewById(R.id.my_scribe_user_level);
			holder.title1 = (TextView) convertView
					.findViewById(R.id.my_scribe_video_title_1);
			holder.title2 = (TextView) convertView
					.findViewById(R.id.my_scribe_video_title_2);
			holder.title3 = (TextView) convertView
					.findViewById(R.id.my_scribe_video_title_3);
			holder.imgVideo1 = (ImageView) convertView
					.findViewById(R.id.my_scribe_video_icon_1);
			holder.imgVideo2 = (ImageView) convertView
					.findViewById(R.id.my_scribe_video_icon_2);
			holder.imgVideo3 = (ImageView) convertView
					.findViewById(R.id.my_scribe_video_icon_3);
			holder.userName = (TextView) convertView
					.findViewById(R.id.my_scribe_nickname);
			holder.videoNum = (TextView) convertView
					.findViewById(R.id.my_scribe_video_num);
			holder.fansName = (TextView) convertView
					.findViewById(R.id.my_scribe_fans_num);
			holder.layoutVideoBack = (RelativeLayout) convertView
					.findViewById(R.id.my_scribe_video_back);
			convertView.setTag(holder);
		}
		final SubscribeMyObj info = infos.get(position);
		final int pos = position;
		if (info.getUser() != null) {
			if (info.getUser().getHeadpic() != null) {
				ImageLoader.getInstance().displayImage(
						info.getUser().getHeadpic(), holder.imgHead,
						Constant.IMAGE_OPTIONS_FOR_USER_COMMON);
//				ImageLoader.getInstance().displayImage(
//						info.getUser().getHeadpic(), holder.imgHead,
//						Constant.IMAGE_OPTIONS_FOR_USER_OTHER);
				
			}
			if (info.getUser().getIsauth().equals("1")) {
				holder.imgV.setVisibility(View.VISIBLE);
			}
			if (info.getUser().getName() != null) {
				holder.userName.setText(info.getUser().getName());
			}
			if (info.getUser().getFan_num() != null
					&& !info.getUser().getFan_num().equals("")) {
				holder.fansName.setText(info.getUser().getFan_num() + "粉丝");
			} else {
				holder.fansName.setText("0粉丝");
			}
			if (info.getUser().getVideo_num() != null
					&& !info.getUser().getVideo_num().equals("")) {
				holder.videoNum.setText(info.getUser().getVideo_num() + "个视频");
			} else {
				holder.videoNum.setText("0个视频");
			}
		}
		if (info.getVideo() != null && info.getVideo().size() > 0) {
			holder.layoutVideoBack.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(
					info.getVideo().get(0).getPic(), holder.imgVideo1,
					Constant.VIDEO_10_16_OPTION);
			holder.title1.setText(info.getVideo().get(0).getTitle());
			android.view.ViewGroup.LayoutParams Params = holder.imgVideo1
					.getLayoutParams();
			Params.width = mWidth; // 设置图片的宽度
			Params.height = mHeight;// 设置图片的高度
			holder.imgVideo1.setLayoutParams(Params);

			android.view.ViewGroup.LayoutParams Params2 = holder.imgVideo2
					.getLayoutParams();
			Params2.width = mWidth; // 设置图片的宽度
			Params2.height = mHeight;// 设置图片的高度
			holder.imgVideo2.setLayoutParams(Params2);
			android.view.ViewGroup.LayoutParams Params3 = holder.imgVideo3
					.getLayoutParams();
			Params3.width = mWidth; // 设置图片的宽度
			Params3.height = mHeight;// 设置图片的高度
			holder.imgVideo3.setLayoutParams(Params3);
			if (info.getVideo().size() <= 1) {
				holder.imgVideo2.setVisibility(View.GONE);
				holder.title2.setVisibility(View.GONE);
				holder.imgVideo3.setVisibility(View.GONE);
				holder.title3.setVisibility(View.GONE);
			} else if (info.getVideo().size() <= 2) {
				holder.imgVideo2.setVisibility(View.VISIBLE);
				holder.title2.setVisibility(View.VISIBLE);
				holder.imgVideo3.setVisibility(View.GONE);
				holder.title3.setVisibility(View.GONE);
				ImageLoader.getInstance().displayImage(
						info.getVideo().get(1).getPic(), holder.imgVideo2,
						Constant.VIDEO_10_16_OPTION);
				holder.title2.setText(info.getVideo().get(1).getTitle());
			} else if (info.getVideo().size() <= 3) {

				holder.imgVideo2.setVisibility(View.VISIBLE);
				holder.title2.setVisibility(View.VISIBLE);
				holder.imgVideo3.setVisibility(View.VISIBLE);
				holder.title3.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						info.getVideo().get(1).getPic(), holder.imgVideo2,
						Constant.VIDEO_10_16_OPTION);
				holder.title2.setText(info.getVideo().get(1).getTitle());
				ImageLoader.getInstance().displayImage(
						info.getVideo().get(2).getPic(), holder.imgVideo3,
						Constant.VIDEO_10_16_OPTION);
				holder.title3.setText(info.getVideo().get(2).getTitle());

			} else {
				holder.layoutVideoBack.setVisibility(View.GONE);
			}

		} else {
			holder.layoutVideoBack.setVisibility(View.GONE);
		}
		holder.imgVideo1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideo() != null && info.getVideo().get(0) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideo().get(0).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						context.startActivity(intent);
					}
				}
			}
		});
		holder.imgVideo2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideo() != null && info.getVideo().get(1) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideo().get(1).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						context.startActivity(intent);
					}
				}
			}
		});
		holder.imgVideo3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideo() != null && info.getVideo().get(2) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideo().get(2).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						context.startActivity(intent);
					}
				}
			}
		});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getUser() != null) {
					Intent intent = new Intent();
					intent.setClass(context, OtherPersonHomeActivity.class);
					intent.putExtra("userId", info.getUser().getUid());
					context.startActivity(intent);
				}
			}
		});
		return convertView;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return infos.get(position);
	}

	@Override
	public int getCount() {
		return infos.size();
	}

	static class ViewHolder {
		TextView title1;
		TextView title2;
		TextView title3;
		ImageView imgVideo1;
		ImageView imgVideo2;
		ImageView imgVideo3;
		ImageView imgHead;
		ImageView imgV;
		TextView videoNum;
		TextView userName;
		TextView fansName;
		RelativeLayout layoutVideoBack;
	}
}
